<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
      >
        <template #toolbar>
          <el-button type="success" size="" @click="openPay(row)">
            批量结账
          </el-button>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="success" size="small" @click="openPay(row)">
              结账
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 已付款操作 -->
    <pay v-model="showPay" :data="current" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import Pay from './components/pay.vue';

  import { EleMessage } from 'ele-admin-plus/es';
  import { pagePayToZrList } from '@/api/company';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      type: 'selection',
      columnKey: 'selection',
      width: 50,
      align: 'center',
      fixed: 'left'
    },
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },
    {
      width: 100,
      prop: 'insurance',
      label: '保险公司'
    },
    {
      width: 100,
      prop: 'insurance_salesman',
      label: '业务员'
    },
    {
      width: 80,
      prop: 'cate',
      label: '类型'
    },
    {
      width: 100,
      prop: 'totalPrice',
      label: '总价'
    },
    {
      width: 100,
      prop: 'price',
      label: '价格'
    },
    {
      width: 100,
      prop: 'otherPrice',
      label: '加价/车船'
    },
    {
      width: 70,
      prop: 'rebate',
      label: '返点'
    },
    {
      width: 100,
      prop: 'rebate_amount',
      label: '返点金额'
    },
    {
      width: 110,
      prop: 'plate_number',
      label: '车牌号码'
    },
    {
      prop: 'company',
      label: '车队'
    },
    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 130,
      align: 'center',
      fixed: 'right'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 付款 */
  const showPay = ref(false);
  const openPay = (row = null) => {
    const rows = row == null ? selections.value : [row];

    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }

    current.value = rows;
    showPay.value = true;
  };
  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pagePayToZrList({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };
</script>
